<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Button;
use addons\BuilderPlus\components\Column;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;
use think\response\Redirect;

class TradeOnline extends Base
{
    public function getOptions(): array
    {
        return [
            'pk' => 'id',
            'model' => \app\common\model\TradeOnline::class,
            'with' => ['user'],
            'append' => ['type_name', 'status_name']
        ];
    }

    public function index(): string
    {
        return Table::init($this)
            ->setCheckbox(false)
            ->addColumns([
                ['id', 'ID', fn(Column $col) => $col->width(80)],
                ['sn', '挂单号', fn(Column $col) => $col->width(180)],
                ['type_name', '类型'],
                ['uid', '相关会员', fn(Column $col) => $col->templet('#_userTpl')->minWidth(240)],
                ['account', '交易账户'],
                ['amount', '交易数量'],
                ['surplus_amount', '剩余交易数量'],
                ['price', '价格'],
                ['create_time', '发布时间', fn(Column $col) => $col->width(150)],
                ['status_name', '状态', fn(Column $col) => $col->fixed('right')]
            ])
            ->addRightButtons([
                'edit' => fn(Button $btn) => $btn->title('查看明细')
                    ->url(url('orderInfo')->build())
                    ->width('90%')
            ])
            ->addSearch([
                ['create_time', '发布时间', fn(Search $search) => $search->type('date')],
                ['type_id', '类型', fn(Search $search) => $search->type('select')->optionValue(getTradeType())],
                ['uid', '会员编号/昵称/手机号', fn(Search $search) => $search->relation('\app\common\model\User', 'code|nickname|mobile,id')->condition('Like')],
                ['status', '状态', fn(Search $search) => $search->type('select')->optionValue(getTradeStatus())]
            ])
            ->enableExport()
            ->fetch();
    }

    public function orderInfo($id): Redirect
    {
        $info = $this->model::where(['id' => $id])->find();
        $url = url('tradeOnlineOrder/index') . '?trade_id=' . $info->sn;

        return redirect($url);
    }
}